<template>
    <div class="category">
        <Header></Header>
        <div class="mb20 fr mr30">
            <el-button type="primary" @click="handleEdit()">添加文章</el-button>
        </div>
        <el-table
                :data="categoryList"
                border
                style="width: 100%">
            <el-table-column
                    prop="title"
                    label="标题">
            </el-table-column>
            <el-table-column
                    prop="articleCategory.title"
                    label="分类">
            </el-table-column>
            <el-table-column
                    label="照片"
            >
                <template slot-scope="scope">
                    <img :src="scope.row.imageUrl" alt="" style="width: 120px;height: 120px;">
                </template>
            </el-table-column>
            <el-table-column
                    label="操作">
                <template slot-scope="scope">
                    <el-button
                            size="mini"
                            @click="handleEdit(scope.row._id)">编辑</el-button>
                    <el-button
                            size="mini"
                            type="danger"
                            @click="handleDelete(scope.row._id)">删除</el-button>
                </template>
            </el-table-column>
        </el-table>
        <cmsPagination :total="total" :pageSize="pageSize" :currentPage="currentPage" @size-change="SizeChange" @current-change="CurrentChange"></cmsPagination>
    </div>
</template>

<script>
    import Header from "../components/Header"
    import cmsPagination from "@/components/cmsPagination";
    export default {
        name: "ArticelList",
        components:{
            Header,
            cmsPagination
        },
        data(){
            return{
                categoryList:[],
                form:{
                    name:''
                },
                dialogAdd:false,
                currentPage:1,
                pageSize:10,
                total:null
            }
        },
        mounted() {
            this.getList()
        },
        methods:{
            getList(){
                this.post(this.$api.articleModel.articleList,{
                    currentPage:this.currentPage,
                    pageSize:this.pageSize
                })
                    .then(res=>{
                        this.categoryList = res.result.list
                        this.total = res.result.total
                    })
            },
            openDialog(){
                this.dialogAdd = true
            },
            handleEdit(){
                this.$router.push('/backstage/article/edit')
            },
            handleDelete(id){
                let userId =id
                this.post(this.$api.articleModel.articleListDelete,{
                    id:id
                })
                    .then(res=>{
                        console.log(res)
                        // this.getList()
                        this.categoryList.forEach((item,index)=>{
                            if (item._id==userId){
                                this.categoryList.splice(index,1)
                                this.total-=1
                                // if(this.categoryList.length<this.pageSize && this.pageSize>this.total){
                                //     this.getList()
                                // }
                            }
                        })
                    })
            },
            SizeChange(val){
                console.log(this.pageSize);
                this.pageSize = val
                this.getList()
            },
            CurrentChange(val){
                this.currentPage = val
                this.getList()
            }
        }
    }
</script>

<style lang="stylus" scoped>
    .category
        width 100%

</style>
